package com.uenpay.im.ui.old.activity.funds;

import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-3-22 下午6:56:47
 * @copyRight http://www.qiyunxin.com
 * @function 获取经费信息列表
 */
public class GetChatFeeTurnoverInvokeItem extends BaseHttpInvokeItem {

	public class GetChatFeeTurnoverInvokeItemResult extends HttpInvokeResult {
		public ArrayList<FundsOutEntity> arrayList = new ArrayList<FundsOutEntity>();
	}

	public GetChatFeeTurnoverInvokeItemResult getOutPut() {
		return (GetChatFeeTurnoverInvokeItemResult) GetResultObject();
	}

	/**
	 * 获取群经费支出、收入信息
	 * 
	 * @param chatid
	 * @param direction
	 *            收支(1为收入，2为支出)
	 */
	public GetChatFeeTurnoverInvokeItem(String chatid, int direction) {
		String url = APIConfiguration.getBaseUrl()
				+ "Chat/SpecialChat/chatFeeTurnoverList?chatid=" + chatid
				+ "&direction=" + direction + "&"
				+ APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		GetChatFeeTurnoverInvokeItemResult result = new GetChatFeeTurnoverInvokeItemResult();
		JSONObject json = null;
		try {
			json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");
			JSONArray dataJson = json.optJSONArray("data");
			if (dataJson != null && dataJson.length() > 0) {
				for (int i = 0, size = dataJson.length(); i < size; i++) {
					result.arrayList.add(desEntity(dataJson.optJSONObject(i)));
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if (json == null) {
			return null;
		}

		return result;
	}

	private FundsOutEntity desEntity(JSONObject json) {
		FundsOutEntity entity = new FundsOutEntity();
		entity.amount = json.optString("amount");
		entity.chatid = json.optString("chatid");
		entity.custid = json.optString("custid");
		entity.direction = json.optString("direction");
		entity.content = json.optString("content");
		entity.id = json.optInt("id");
		entity.createtime = json.optString("createtime");
		return entity;
	}

}
